package com.dhcc.bpm.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.system.entity.TopMenu;
import com.dhcc.bpm.modules.system.service.ITopMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.constant.AuthConstant;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.Func;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;

/**
 * @ClassName TopMenuController
 * @Description 顶部菜单表 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/topmenu")
@Api(value = "顶部菜单表", tags = "顶部菜单")
@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
public class TopMenuController extends BladeController {

	private ITopMenuService topMenuService;

	/**
	 * @Description 详情
	 * @Author sjm
	 * @Date 10:08 2020/2/22 0022
	 * @param topMenu
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.entity.TopMenu>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入topMenu")
	@ApiLog("查询顶部菜单详情")
	@Log(title = "查询顶部菜单详情", businessType = BusinessType.SELECT)
	public R<TopMenu> detail(TopMenu topMenu) {
		TopMenu detail = topMenuService.getOne(Condition.getQueryWrapper(topMenu));
		return R.data(detail);
	}

	/**
	 * @Description 分页 顶部菜单表
	 * @Author sjm
	 * @Date 10:08 2020/2/22 0022
	 * @param topMenu
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.entity.TopMenu>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@PreAuth(AuthConstant.PERMISSION_ALL)
	@ApiOperation(value = "分页", notes = "传入topMenu")
	@ApiLog("分页查询顶部菜单表")
	@Log(title = "查询顶部菜单表", businessType = BusinessType.SELECT)
	public R<IPage<TopMenu>> list(@ApiIgnore @RequestParam Map<String, Object> topMenu, Query query) {
		IPage<TopMenu> pages = topMenuService.page(Condition.getPage(query), Condition.getQueryWrapper(topMenu,TopMenu.class));
		return R.data(pages);
	}

	/**
	 * @Description  新增 顶部菜单表
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param topMenu
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入topMenu")
	@ApiLog("新增顶部菜单")
	@Log(title = "顶部菜单", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody TopMenu topMenu) {
		return R.status(topMenuService.save(topMenu));
	}

	/**
	 * @Description 修改 顶部菜单表
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param topMenu
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入topMenu")
	@ApiLog("修改顶部菜单")
	@Log(title = "顶部菜单", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody TopMenu topMenu) {
		return R.status(topMenuService.updateById(topMenu));
	}

	/**
	 * @Description 新增或修改 顶部菜单表
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param topMenu
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入topMenu")
	@ApiLog("新增或修改顶部菜单")
	@Log(title = "顶部菜单", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody TopMenu topMenu) {
		return R.status(topMenuService.saveOrUpdate(topMenu));
	}


	/**
	 * @Description 删除 顶部菜单表
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("删除顶部菜单")
	@Log(title = "顶部菜单", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(topMenuService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * @Description 设置顶部菜单
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param topMenuIds
	 * @param menuIds
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/grant")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "顶部菜单配置", notes = "传入topMenuId集合以及menuId集合")
	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
	@ApiLog("设置顶部菜单")
	@Log(title = "顶部菜单", businessType = BusinessType.UPDATE)
	public R grant(@ApiParam(value = "topMenuId集合", required = true) @RequestParam String topMenuIds,
				   @ApiParam(value = "menuId集合", required = true) @RequestParam String menuIds) {
		boolean temp = topMenuService.grant(Func.toLongList(topMenuIds), Func.toLongList(menuIds));
		return R.status(temp);
	}

}
